package com.yiwenup.exception.global;

import com.yiwenup.common.basic.BaseResponseDTO;
import com.yiwenup.exception.basic.BaseException;
import com.yiwenup.exception.enums.ExceptionEnum;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常
 * TODO 全局异常信息国际化
 **/
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 全局异常 - 系统业务层抛出处理
     *
     * @return 响应信息
     */
    @ResponseStatus(HttpStatus.OK)
    @ExceptionHandler(BaseException.class)
    public BaseResponseDTO<Object> handleSystemException(BaseException e) {
        BaseResponseDTO<Object> resp = BaseResponseDTO.fail();
        resp.setRespCode(e.getErrorCode());
        resp.setRespMsg(e.getErrorMsg());
        return resp;
    }

    /**
     * 全局异常托底捕获
     *
     * @return 响应信息
     */
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Throwable.class)
    public BaseResponseDTO<Object> handleUnknownException() {
        BaseResponseDTO<Object> resp = BaseResponseDTO.fail();
        resp.setRespCode(ExceptionEnum.SYSTEM_EXCEPTION.getCode());
        resp.setRespMsg(ExceptionEnum.SYSTEM_EXCEPTION.getDesc());
        return resp;
    }
}
